﻿using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.IO;
public class PMTool  {
	#region 底层操作
	public static void CreateFile(string path){
		Directory.CreateDirectory (path);
	}

	public static string[] GetDirectories(string path){
		return Directory.GetDirectories (path);
	}

	public static bool HasFile(string path){
		FileInfo file = new FileInfo (path);
		return file.Exists;
	}

	public static string[] GetFiles(string path)
	{
		return Directory.GetFiles (path);
	}

	public static void DeleFile(string path){
		try{
		Directory.Delete (path,true);
		}catch{
		}
	}

	public static void RefreshAssets(){
		AssetDatabase.Refresh ();
	}

	public static void SaveScript(string path,string value){
		FileInfo info = new FileInfo (path);
		if (info.Exists)
			info.Delete ();
		
		StreamWriter writer = info.CreateText ();
		writer.Write (value);
		writer.Close ();
	}

	public static GameObject LoadUIRoot(){
		GameObject uiroot = null;
		try{
			UIRoot root = GameObject.FindObjectOfType<UIRoot>();
			if(root !=null)
				uiroot = root.gameObject;
		}catch{}
		if(uiroot == null)
			uiroot = ObjManager.Instance.AddChild("Prefab/Other/","UI Root");
		return uiroot;
	}

	public static void SavePreafab(GameObject obj,string path){
		path += ".prefab";
		path = path.Replace (Application.dataPath, "Assets");
		Debug.Log (" save prefab path:" + path);
		PrefabUtility.CreatePrefab(path,obj);
	}

	public static GameObject GetObjWithComponent(string path,string objectname){
		GameObject obj = new GameObject (objectname);
		MonoScript script;
		path = path.Replace(Application.dataPath,"Assets")+objectname+".cs";
		Debug.Log (path);
		script = AssetDatabase.LoadAssetAtPath<MonoScript>(path);
		System.Type type;
		type = script.GetClass();
		obj.AddComponent(type);
		return obj;
	}

	public static string ReplaceScript(string source,Dictionary<string,string> r){
		foreach (var k in r) {
			source = source.Replace ("%" + k.Key, k.Value);
		}
		return source;
	}

	public static string LoadText(string path){
		FileInfo info = new FileInfo (path);
		StreamReader reader = info.OpenText ();
		string value = reader.ReadToEnd ();
		reader.Close ();
		return value;
	}
	public static string GetUIComponentTag<T>() where T:Component{
		string value;
		string typename = typeof(T).Name;
		typename = typename.Replace ("UI", "");
		value = typename.Substring (0, 1).ToUpper () + typename.Substring (1, 2).ToLower () + "_";
		if (typeof(T).Name == "BoxCollider")
			value = "btn_";
		return value;
	}
	#endregion

	#region　操作界面
	public static void SaveView(string path,string name){
		GameObject obj = GameObject.Find (name);
		if (obj != null) {
			SavePreafab (obj, path+name);
		}
		try{
			GameObject uiroot = GameObject.FindObjectOfType<ObjManager>().gameObject;
			if (uiroot != null)
				GameObject.DestroyImmediate (uiroot);
		}catch{
		}
	}

	public static void OpenFile<T>(string path) where T:UnityEngine.Object{
		path = path.Replace (Application.dataPath, "Assets");
		var script = AssetDatabase.LoadAssetAtPath<T> (path);
		AssetDatabase.OpenAsset (script);
	}

	public static void OpenView(string path){
		path += ".cs";
		OpenFile<MonoScript> (path);
	}

	public static GameObject LoadView(string path,string name){
		GameObject viewObj;
		viewObj = GameObject.Find (name);
		if (viewObj == null) {
			GameObject uiroot = LoadUIRoot ();
			Debug.Log (" Load View:" + path + name);
			viewObj = ObjManager.Instance.AddChild (path, name);
			viewObj.transform.parent = uiroot.transform;
			viewObj.gameObject.transform.localScale = Vector3.one;
			try{
				viewObj.gameObject.layer = LayerMask.NameToLayer ("UI");
			}catch{
			}
			Selection.activeGameObject = viewObj;
		}
		return viewObj;
	}

	public static void UpdateBaseView(string path,string name){
		PMToolTemplate.FixViewUI (path, name);

		string viewname = name.Split ('_') [0];
		PMToolTemplate.CreateScript_View (path, viewname,true);
		PMToolTemplate.CreateScript_ViewModel (path, viewname,true);
		PMToolTemplate.CreateScript_Model (path, viewname,true);

		PMToolTemplate.FixBaseView_ViewMode_Mode (path, name);
	}


	#endregion

	#region Create

	public static void CreateScene(string scenename){
		UnityEditor.SceneManagement.EditorSceneManager.NewScene (UnityEditor.SceneManagement.NewSceneSetup.EmptyScene);
		EditorApplication.SaveScene ("Assets/Scene/"+scenename+".unity");
	}

	public static void DeleScene(string scenename){
		UnityEditor.SceneManagement.EditorSceneManager.NewScene (UnityEditor.SceneManagement.NewSceneSetup.EmptyScene);
		AssetDatabase.DeleteAsset ("Assets/Scene/" + scenename+".unity");
	}



	public static void CreateScriptModulePanel(string path,string name){
//		PMToolTemplate.CreateScriptModuleCtrl (path, name);
		PMToolTemplate.CreateScript_View (path, name);
		PMToolTemplate.CreateScript_ViewModel (path, name);
		PMToolTemplate.CreateScript_Model (path, name);

	}

	public static void CreateModule(string name){
		CreateFile (PMConst.PathModule + name);
		CreateFile (PMConst.PathModule + name + "/Panel");
		CreateFile (PMConst.PathModule + name + "/MVC");
		CreateFile (PMConst.PathPrefab+ name);
		PMToolTemplate.CreateScriptModuleCtrl (PMConst.PathModule + name + "/MVC/",name);
		CreateScene (name);
		RefreshAssets ();
	}
	public static void DeleModule(string name){
		DeleFile (PMConst.PathModule + name + "/Panel");
		DeleFile (PMConst.PathModule + name + "/MVC");
		DeleFile (PMConst.PathModule + name);
		DeleFile (PMConst.PathPrefab+ name);
		DeleFile (name);
		DeleScene (name);
		RefreshAssets ();
	}

	public static void OnComingFinish(string clipname,string viewname){
		GameObject viewobj = GetObjWithComponent (PMConst.PathModule + clipname + "/Panel/", viewname);
		GameObject uiroot = LoadUIRoot ();
		if (viewobj == null)
			Debug.Log (" viewobj is null");
		if (uiroot == null)
			Debug.Log (" uiroot is null");
		viewobj.transform.parent = uiroot.transform;
		viewobj.gameObject.AddComponent<UIPanel> ();
		viewobj.gameObject.AddComponent<MyOBJ> ();
		clipname = clipname.Replace ("Module", "");
		SavePreafab (viewobj, PMConst.PathPrefab+clipname+"/" + viewobj.name);
	}
	#endregion
}
